/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head==nullptr||head->next==nullptr) return false;
        ListNode* p1=head, *p2=head;
        p1=head->next, p2=head->next->next;
        while(p1&&p2)
        {
            if(p1==p2) return true;
            p1=p1->next; p2=p2->next;
            if(p2) p2=p2->next;
            else return false;
        }
        return false;
        
    }
};